




class Solution:

    def merge(self, intervals: list[list[int]]):

        if not intervals:
            return [[]]
        
        n = len(intervals)

        intervals.sort(key=lambda it : it[0])

        # print(intervals)
        ans = []
        temp = intervals[0]
        # for i in range(n - 1):
        i = 1
        while i < n:
            # 合并
            if temp[1] >= intervals[i][0]:

                if temp[1] < intervals[i][1]:
                    temp[1] = intervals[i][1]
                # i += 1
            else:

                ans.append(temp)
                # temp.clear()
                temp = intervals[i]
            
            i += 1

        ans.append(temp)

        return ans    


    


if __name__ == "__main__":

    intervals = [[1,3],[2,6],[8,10],[15,18]]

    sol = Solution()
    ans = sol.merge(intervals)

    print(ans)


